function mindist = mindistGrpOfferGS(theta,match)

NN = match.NN;
JJ = match.JJ;
J = match.J;
N = match.N/J;
M = match.M;
S = match.S;

% Parcel preferences
gamma = [1 theta(1,1:NN - 1)];
% Firm preferences
delta = [1 theta(1,NN:NN + JJ - 2)];

NN = size(gamma,2); JJ = size(delta,2);

% MarketCount0 = match.MarketCount0;

% Firm Value of parcels
ParcelUtility = match.ParcelIndex*gamma';
% Parcel value of firms (and leases)
FirmUtility = match.FirmMatrix*delta';

%% Market Match

SampleMMean_SimMoment       = zeros(M,JJ + NN,S);
SampleMVar_SimMoment        = zeros(M,JJ + NN,S);
SampleMCov_SimMoment        = zeros(M,JJ*NN,S);

SampleJMean_SimMoment       = zeros(J,NN,S);
SampleJCov_SimMoment        = zeros(J,NN*JJ,S);
SampleJVar_SimMoment        = zeros(J,NN,S);
SampleJDist_SimMoment       = zeros(J,NN*JJ,S);

SampleJMMean_SimMoment      = zeros(J*M,NN,S);
SampleJMVar_SimMoment       = zeros(J*M,NN,S);

SampleMeanGroup_SimMoment   = zeros(M,NN,S);
SampleVarGroup_SimMoment    = zeros(M,NN,S);

SampleJntDist_SimMoment     = zeros(NN*JJ,S);
SampleCov_SimMoment         = zeros(NN*JJ,S);

parfor s = 1:S
    
    % Sub-market profits and index
    Offer_sub = ParcelUtility + match.ParcelError_Sim(:,s); 
    Accept_sub = FirmUtility + match.FirmError_Sim(:,s); 

    Aoffer = reshape(Offer_sub,[J,N]); 
    Baccept = reshape(Accept_sub,[J,N]);

    % Sort preferences
    rankA = SortingMatInC(Aoffer); 
    rankB = SortingResMatInC(Baccept);
        
    % GS algorithm
    [~,FirmMatchOUT] = MatchAofferBaccept_GS_out(rankA,rankB,match.FirmMatchConstraint);

    % Firm group royalty/clause means for moments
    [~,~,PMean,FMean,JntDist,Cov,Pvar,Fvar,PFcovar,Pmean_firm,Pvar_firm,PFJntDist_firm,PFcovar_firm,GpMean,GpVar,GpMean_byJ,GpVar_byJ] = GroupMomentsSmall_new(match.ParcelIndex,match.FirmMatrix,FirmMatchOUT,J,match.MarketIndex0,match.MarketCount0,match.MomentSwitch);
     
    % Calculate the moments
    SampleMMean_SimMoment(:,:,s)        = [FMean PMean];
    SampleMCov_SimMoment(:,:,s)         = PFcovar;
    SampleMVar_SimMoment(:,:,s)         = [Fvar Pvar];

    SampleJMean_SimMoment(:,:,s)        = Pmean_firm;
    SampleJCov_SimMoment(:,:,s)         = PFcovar_firm;
    SampleJVar_SimMoment(:,:,s)         = Pvar_firm;
    SampleJDist_SimMoment(:,:,s)        = PFJntDist_firm;
        
    SampleJntDist_SimMoment(:,s)        = JntDist;
    SampleCov_SimMoment(:,s)            = Cov;
end

clear rankA rankB Aoffer Baccept shareOUT shareStOUT
clear FirmUtility_sub ParcelUtility_sub

clear row m index s temp Group

JntDist_SimMoment    = sum(SampleJntDist_SimMoment,2)*(1/S);
Cov_SimMoment        = sum(SampleCov_SimMoment,2)*(1/S);

% Market Moments
MMean_SimMoment      = sum(SampleMMean_SimMoment,3)*(1/S);
MCov_SimMoment       = sum(SampleMCov_SimMoment,3)*(1/S);
MVar_SimMoment       = sum(SampleMVar_SimMoment,3)*(1/S);

% Firm Moments
JMean_SimMoment      = sum(SampleJMean_SimMoment,3)*(1/S);
JCov_SimMoment       = sum(SampleJCov_SimMoment,3)*(1/S);
JVar_SimMoment       = sum(SampleJVar_SimMoment,3)*(1/S);
JDist_SimMoment      = sum(SampleJDist_SimMoment,3)*(1/S);


M_sim = [JntDist_SimMoment; Cov_SimMoment; ...
    MMean_SimMoment(:); MCov_SimMoment(:); MVar_SimMoment(:); ...
    JMean_SimMoment(:); JCov_SimMoment(:); JVar_SimMoment(:); JDist_SimMoment(:)];

M = [match.JntDist0; match.Cov0; ...
    match.MMean_Moment; match.MCov_Moment; match.MVar_Moment; ...
    match.JMean_Moment; match.JCov_Moment; match.JVar_Moment; match.JDist_Moment];

mindist = norm(M - M_sim);
